package com.hbx.residential_service.dao;

import com.hbx.residential_service.domain.Admin;
import com.hbx.residential_service.domain.Repair;
import com.hbx.residential_service.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RepairDao {
    @Select("select * from repairs")
    @Results({
            @Result(id=true,property = "rpId",column = "rpId"),
            @Result(property = "rpTitle",column = "rpTitle"),
            @Result(property = "rpContent",column = "rpContent"),
            @Result(property = "rpDate",column = "rpDate"),
            @Result(property = "rpStatus",column = "rpStatus"),
            @Result(property = "rpHandleDate",column = "rpHandleDate"),

            @Result(property = "user",column = "rpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "rpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    List<Repair> findAll(int page, int size);

    @Select("select * from repairs where rpId=#{rpId}")
    @Results({
            @Result(id=true,property = "rpId",column = "rpId"),
            @Result(property = "rpTitle",column = "rpTitle"),
            @Result(property = "rpContent",column = "rpContent"),
            @Result(property = "rpDate",column = "rpDate"),
            @Result(property = "rpStatus",column = "rpStatus"),
            @Result(property = "rpHandleDate",column = "rpHandleDate"),

            @Result(property = "user",column = "rpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "rpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    Repair findDetailById(int rpId);

    @Delete("delete from repairs where rpId=#{rpId}")
    void deleteRepair(int rpId);

    @Update("update repairs set rpStatus=#{rpStatus},rpHandleDate=#{rpHandleDate},rpUserId=#{user.userid},rpAdminId=#{admin.adminid} where rpId=#{rpId}")
    void UpdateRPById(Repair repair);

    @Select("select * from repairs where rpUserId in (select userid from owners where username like #{content})")
    @Results({
            @Result(id=true,property = "rpId",column = "rpId"),
            @Result(property = "rpTitle",column = "rpTitle"),
            @Result(property = "rpContent",column = "rpContent"),
            @Result(property = "rpDate",column = "rpDate"),
            @Result(property = "rpStatus",column = "rpStatus"),
            @Result(property = "rpHandleDate",column = "rpHandleDate"),

            @Result(property = "user",column = "rpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "rpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    List<Repair> fsByUsername(@Param("content") String content,@Param("page") int page, @Param("size") int size);

    @Select("select * from repairs where rpStatus=0")
    @Results({
            @Result(id=true,property = "rpId",column = "rpId"),
            @Result(property = "rpTitle",column = "rpTitle"),
            @Result(property = "rpContent",column = "rpContent"),
            @Result(property = "rpDate",column = "rpDate"),
            @Result(property = "rpStatus",column = "rpStatus"),
            @Result(property = "rpHandleDate",column = "rpHandleDate"),

            @Result(property = "user",column = "rpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "rpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    List<Repair> findByUnHandle(int page, int size);
}
